import { getInfo, loginByCode, loginByPwd } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    username: '',
    avatar: '',
    companyName: '',
    companyId: '',
    employerId: '',
    email: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_COMPANY_NAME: (state, companyName) => {
    state.companyName = companyName
  },
  SET_COMPANY_ID: (state, companyId) => {
    state.companyId = companyId
  },
  SET_EMPLOYER_ID: (state, employerId) => {
    state.employerId = employerId
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  }
}

const actions = {
  // 密码登录
  loginByPwd({ commit }, loginData) {
    return new Promise((resolve, reject) => {
      loginByPwd(loginData).then(response => {
        const { data, errMessage } = response
        if (!data) {
          reject(errMessage || '身份认证信息过期,请重新登录')
        }
        commit('SET_TOKEN', response.data)
        setToken(response.data)
        resolve()
      }).catch((error) => {
        reject(error)
      })
    })
  },
  // 验证码登录
  loginByCode({ commit }, loginData) {
    return new Promise((resolve, reject) => {
      loginByCode(loginData).then(response => {
        const { data, errMessage } = response
        if (!data) {
          reject(errMessage || '身份认证信息过期,请重新登录')
        }
        commit('SET_TOKEN', response.data)
        setToken(response.data)
        resolve()
      }).catch((error) => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getInfo().then(response => {
        const { data } = response
        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        const { username, avatar, companyId, companyName, id, email } = data
        commit('SET_EMPLOYER_ID', id)
        commit('SET_USERNAME', username)
        commit('SET_AVATAR', avatar)
        commit('SET_COMPANY_ID', companyId)
        commit('SET_COMPANY_NAME', companyName)
        commit('SET_EMAIL', email)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve) => {
      commit('SET_TOKEN', '')
      removeToken() // must remove  token  first
      resetRouter()
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

